package StackAndQueuePackage;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author Lzm
 * @version 1.0
 */
public class maxSlidingWindow_ {

  @Test
  public void maxSlidingWindowTest(){
    int[] nums = new int[]{1,3,1,2,0,5};
    int k = 3;
    int[] ints = maxSlidingWindow(nums, k);
    for (int anInt : ints) {
      System.out.println(anInt);
    }
  }

  public int[] maxSlidingWindow(int[] nums, int k) {
    int[] result = new int[nums.length - k + 1];
    if (nums.length == 1){
      return new int[]{nums[0]};
    }
    Deque<Integer> queue = new LinkedList<>();

    for (int i = 0 ; i< k ; i++){
      add(queue,nums[i]);
    }
    int index = 0;
    result[index++] = queue.peek();
    for (int i = k ; i < nums.length ; i++){
      poll(queue,nums[i - k]);
      add(queue,nums[i]);

      result[index++] = queue.peek();
    }
    return result;
  }

  public void add(Queue<Integer> queue,int num){/*
    while(!queue.isEmpty() && queue.getLast() < num){
      //queue.removeLast();
    }*/
      queue.add(num);
    }
  public void poll(Queue<Integer> queue,int num){
    if (!queue.isEmpty() && queue.peek() == num){
      queue.poll();
    }
  }
}
